package leetcode;

public class RotateImage {

    public static void main(String[] args) {
        RotateImage rotateImage = new RotateImage();
        int arr[][] = new int[4][4];
        arr[0] = new int[]{1, 2, 3, 88};
        arr[1] = new int[]{4, 5, 6, 88};
        arr[2] = new int[]{7, 8, 9, 88};
        arr[3] = new int[]{10, 11, 12, 88};
        rotateImage.rotate(arr);

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 1, 2, 3
     * 4, 5, 6
     * 7, 8, 9
     *
     * 7, 4, 1
     * 8, 5, 2
     * 9, 6, 3
     *
     * arr[0,0] -> arr[0, n] / arr[0, 1] -> arr[n, 1] / arr[0, n] -> arr[n, n]
     * arr[1,0] -> arr[0, 1] / arr[1, 1] -> arr[1, 1] / arr[1, n] -> arr[n, n]
     * arr[n,0] -> arr[0, 0] / arr[n, 1] -> arr[0, 1] / arr[n, n] -> arr[0, n]
     * @param matrix
     */
    public void rotate(int[][] matrix) {
        int [][]new_matrix = new int[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                new_matrix[j][matrix.length - i - 1] = matrix[i][j];
            }
        }
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = new_matrix[i][j];
            }
        }
    }

}
